package model

import (
	"database/sql/driver"
	"errors"

	"golang.org/x/crypto/bcrypt"
)

type IToken interface {
	Id() uint
}

type MPassword string

func (p *MPassword) Scan(value interface{}) error {
	v, ok := value.([]byte)
	if !ok {
		return errors.New("密码转换出错")
	}
	*p = MPassword(v)
	return nil
}

func (p MPassword) Value() (driver.Value, error) {
	hashed, err := bcrypt.GenerateFromPassword([]byte(p), bcrypt.DefaultCost)
	return string(hashed), err
}
